package com.cskaoyan.kpi.peak;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName DailyHourPeakMapper
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/2/17 9:12
 * @Version V1.0
 **/
public class DailyHourPeakMapper extends Mapper<Object, Text, IntWritable,Text> {

    @Override
    protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
        //106.87.2.229---中国:重庆市:未知---2022-01-14 08:01:53---http://115.29.141.32:8085/#/mall/show/goodsList/190/all---firefox
        String line = value.toString();
        String[] parts = line.split("---");
        String datetime = parts[2];
        try {
            Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(datetime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int hours = calendar.get(Calendar.HOUR_OF_DAY);
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int d = calendar.get(Calendar.DATE);
            context.write(new IntWritable(hours), new Text(year + "-" + month + "-" + d));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
